package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Member;
import com.powernode.model.Result;
import com.powernode.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 管理系统：会员业务控制层
 */
@Api(tags = "管理系统会员接口管理")
@RequestMapping("admin/user")
@RestController
public class SysMemberController {

    @Autowired
    private MemberService memberService;


    @ApiOperation("多条件分页查询会员")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:user:page')")
    public Result<Page<Member>> loadMemberPage(Page<Member> page, Member member) {
        page = memberService.page(page,new LambdaQueryWrapper<Member>()
                .eq(ObjectUtil.isNotNull(member.getStatus()),Member::getStatus,member.getStatus())
                .like(StringUtils.hasText(member.getNickName()),Member::getNickName,member.getNickName())
                .orderByDesc(Member::getCreateTime)
        );
        return Result.success(page);
    }

//    admin/user/info/5
    @ApiOperation("查询会员的信息")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('admin:user:info')")
    public Result<Member> loadMemberInfo(@PathVariable Long id) {
        Member member = memberService.getById(id);
        return Result.success(member);
    }

    @ApiOperation("修改会员的状态")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:user:update')")
    public Result<String> modifyMemberStatus(@RequestBody Member member) {
        boolean updated = memberService.updateById(member);
        return Result.handle(updated);
    }


    ///////////////////////// feign 接口 ////////////////////////////////
    @ApiOperation("查询会员的昵称")
    @GetMapping("getMemberNickNameByOpenId")
    public Result<String> getMemberNickNameByOpenId(@RequestParam String openId) {
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .select(Member::getNickName)
                .eq(Member::getOpenId, openId)
        );
        return Result.success(member.getNickName());
    }
}
